<template>
  <Edialog
    title="修改参数"
    :outershow="dialogShow"
    @close="close"
    saveClick
    @saveFn="saveFn"
  >
    <div style="padding: 0 20px">
      <el-form ref="form" :model="form" label-width="100px" :rules="fromRules">
        <el-form-item label="参数编号">
          <el-input
            v-model="form.paramCode"
            placeholder="请输入参数编号"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="参数描述">
          <el-input
            v-model="form.paramName"
            placeholder="请输入参数描述"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="参数值" prop="paramValue" v-if="showEdit == 0">
          <el-input
            v-model="form.paramValue"
            placeholder="请输入参数值"
          ></el-input>
        </el-form-item>
        <el-form-item label="参数值" v-else>
          <EwangEditor
            ref="editor"
            :html="form.paramValue"
            @getHtml="getHtml"
            :isClose="true"
          />
        </el-form-item>
      </el-form>
    </div>
  </Edialog>
</template>

<script>
import Edialog from "@/components/Edialog";
import {
  postSysSystemParam,
  getSysSystemParamEdit,
} from "@/api/management/parameter";
import { fd } from "@/utils";
import EwangEditor from "@/components/EwangEditor";
export default {
  components: {
    Edialog,
    EwangEditor,
  },
  data() {
    return {
      dialogShow: false,
      form: {
        paramCode: "",
        paramName: "",
        paramValue: "",
      },
      fromRules: {
        paramValue: [
          { required: true, message: "请输入正确的参数值", trigger: "blur" },
          { pattern: "", message: "请輸入符合校验的格式参数", trigger: "blur" },
        ],
      },
      showEdit: 0,
    };
  },
  methods: {
    async open(row) {
      let res = await getSysSystemParamEdit(row.PARAM_CODE);
      if (res.data && res.data.bcode == 0) {
        setTimeout(() => {
          this.form.paramValue = res.data.bdata.paramValue;
        });
      }
      this.showEdit = row.IS_EDITOR;
      // 后期手动修改，校验规则在 row 里面的字段
      this.fromRules.paramValue[1].pattern = /^[0-9]*$/;
      this.form.paramCode = row.PARAM_CODE;
      this.form.paramName = row.PARAM_NAME;
      this.dialogShow = true;
    },
    async saveFn() {
      let res = await postSysSystemParam(fd(this.form));
      console.log(res);
      if (res.data && res.data.bcode == 0) {
        this.$message.success("操作成功");
        this.$parent.getData();
        this.dialogShow = false;
      }
    },
    close() {
      this.$refs.form.clearValidate();
      this.dialogShow = false;
    },
    getHtml(data) {
      console.log("编辑器数据", data);
      if (data && data !== "<p><br></p>") {
        console.log("执行赋值");
        this.form.paramValue = data;
      }
    },
  },
};
</script>

<style scoped lang="scss">
.top {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  .title {
    width: 90px;
  }
  .el-input {
    width: 250px;
  }
  .el-button--primary {
    margin-left: 50px;
  }
}
</style>
